package com.minsf.springbootinit.model.vo;

import cn.hutool.core.bean.BeanUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minsf.springbootinit.model.dto.question.JudgeCase;
import com.minsf.springbootinit.model.dto.question.JudgeConfig;
import com.minsf.springbootinit.model.entity.Question;
import lombok.Data;

import java.util.List;

/**
 * @Description
 * @Author minify
 * @Date 2024/1/3 20:24
 * @Project oj-backend
 */
@Data
public class QuestionVO {
    /**
     * id
     */
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目答案
     */
    private String answer;

    /**
     * 判题配置（json对象）
     */
    private JudgeConfig judgeConfig;

    /**
     * 判题用例（json数组）
     */
    private List<JudgeCase> judgeCase;

    /**
     * 创建用户 id
     */
    private Long userId;

    private UserVO user;

    private static final long serialVersionUID = 1L;

    public static QuestionVO objToVo(Question question) {
        QuestionVO questionVO = new QuestionVO();
        BeanUtil.copyProperties(question, questionVO, "judgeCase", "judgeConfig");
        String judgeCaseStr = question.getJudgeCase();
        String judgeConfigStr = question.getJudgeConfig();

        Gson gson = new Gson();
        List<JudgeCase> judgeCases = gson.fromJson(judgeCaseStr, new TypeToken<List<JudgeCase>>(){}.getType());
        questionVO.setJudgeCase(judgeCases);
        JudgeConfig judgeConfig = gson.fromJson(judgeConfigStr, JudgeConfig.class);
        questionVO.setJudgeConfig(judgeConfig);
        return questionVO;
    }
}
